home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.04 Apr 93 / Creating EPSF Files / WriteEPSF.c < prev   
Encoding:
C/C++ Source or Header  |  1992-07-15  |  3.2 KB  |  132 lines  |  [TEXT/KAHL]

  1. /* WriteEPSF.c */
  2. /* Low level routines for buffering output */
  3. /* Copyright 1992, Gary D. McGath */
  4.  
  5. #define BUFFSIZE 512
  6.  
  7. void InitOutBuf(void);
  8. void OutputString(char *str, int theFile);
  9. void OutputChar(int ch,int theFile);
  10. void OutputNum(int val, int theFile);
  11. void FlushOutBuf(int theFile);
  12. static void OutputHexNibble(int nib, int theFile);
  13.  
  14. static char buff[BUFFSIZE];        /* output buffer */
  15. static int buffoff;            /* current offset into buffer */
  16.  
  17. /* This must be called before doing any buffered I/O */
  18. void InitOutBuf()
  19. {
  20.     buffoff = 0;            /* make empty buffer */
  21. }
  22.  
  23. /* Write a C string to the file */
  24. void OutputString(char *str, int theFile)
  25. {
  26.     while (*str)
  27.         OutputChar(*str++,theFile);
  28. }
  29.  
  30. /* write a number to the file, in decimal, with a trailing space */
  31. void OutputNum(int val, int theFile)
  32. {
  33.     int dvsr = 10000;
  34.     int qtnt;
  35.     int leadflag = 0;            /* 0 if no significant digits yet */
  36.     if (val < 0) {
  37.         val = -val;
  38.         OutputChar('-',theFile);    /* take care of negative nos. */
  39.     }
  40.     while (dvsr > 0) {
  41.         if (dvsr == 1)
  42.             leadflag = 1;            /* always do at least one digit */
  43.         qtnt = val/dvsr;
  44.         if (qtnt == 0) {
  45.             if (leadflag)            /* need to output a zero? */
  46.                 OutputChar('0',theFile);
  47.         }
  48.         else {
  49.             OutputChar(qtnt + '0', theFile);    /* output a digit */
  50.             leadflag = 1;            /* now we've seen a leading digit */
  51.         }
  52.         val = val - qtnt * dvsr;    /* take the remainder */
  53.         dvsr /= 10;                    /* down one decimal place */
  54.     }
  55.     OutputChar(' ', theFile);        /* trailing space */
  56. }
  57.  
  58. /* OutputDouble -- similar in concept to OutputNum. Doesn't handle huge
  59.    or tiny numbers reasonably. */
  60. void OutputDouble(double val, int theFile)
  61. {
  62.     long ival;
  63.     long dvsr = 1000000;
  64.     int qtnt;
  65.     int leadflag = 0;            /* 0 if no significant digits yet */
  66.  
  67.     if (val < 0.001 && val > -0.001) {        /* special case for zero or almost */
  68.         OutputNum(0, theFile);
  69.         return;
  70.     }
  71.     ival = val * 1000;
  72.     if (ival < 0) {
  73.         ival = -ival;
  74.         OutputChar('-',theFile);    /* take care of negative nos. */
  75.     }
  76.     while (dvsr > 0) {
  77.         if (dvsr == 100) {
  78.             OutputChar ('.', theFile);    /* the decimal point */
  79.             leadflag = 1;            /* always do zeroes in fraction */
  80.         }
  81.         qtnt = ival/dvsr;
  82.         if (qtnt == 0) {
  83.             if (leadflag)            /* need to output a zero? */
  84.                 OutputChar('0',theFile);
  85.         }
  86.         else {
  87.             OutputChar(qtnt + '0', theFile);    /* output a digit */
  88.             leadflag = 1;            /* now we've seen a leading digit */
  89.         }
  90.         ival = ival - qtnt * dvsr;    /* take the remainder */
  91.         dvsr /= 10;                    /* down one decimal place */
  92.         if (ival == 0 && dvsr <= 100)
  93.             break;                    /* leave out trailing non-significant zeroes */
  94.     }
  95.     OutputChar(' ', theFile);        /* trailing space */
  96.     
  97. }
  98.  
  99. void OutputHex(int ch, int theFile)
  100. {
  101.     OutputHexNibble(ch >> 4, theFile);
  102.     OutputHexNibble(ch, theFile);
  103. }
  104.  
  105. static void OutputHexNibble(int nib, int theFile)
  106. {
  107.     nib &= 0XF;
  108.     if (nib > 9)            /* A-F */
  109.         OutputChar(nib - 10 + 'A', theFile);
  110.     else OutputChar(nib + '0', theFile);
  111. }
  112.  
  113. /* Write one character to the file */
  114. void OutputChar(int ch, int theFile)
  115. {
  116.     long count = BUFFSIZE;
  117.     buff[buffoff++] = ch;
  118.     if (buffoff >= BUFFSIZE) {
  119.         FSWrite(theFile,&count, buff);
  120.         buffoff = 0;
  121.     }
  122. }
  123.  
  124. /* Write out whatever is left in the buffer. This must be called before
  125.    closing. */
  126. void FlushOutBuf(int theFile)
  127. {
  128.     long count;
  129.     count = buffoff;
  130.     FSWrite(theFile,&count, buff);
  131. }
  132.